package com.ccl.iot.template;import android.content.Context;
import android.graphics.Canvas;
import android.view.MotionEvent;
import android.view.View;
import android.widget.ListView;

public class StandardListView extends ListView {
	private boolean iHaveTouchedChild = false;
	private boolean iDrawAfterTouchChild = false;
	
	public StandardListView(Context context) {
		super(context);
	}

	@Override
	protected void onDraw(Canvas canvas) {
		if (iHaveTouchedChild){
			iDrawAfterTouchChild = true;
		}else{
			iDrawAfterTouchChild = false;
		}
		
		super.onDraw(canvas);
	}
	
	@Override
	public boolean onInterceptTouchEvent(MotionEvent ev) {
		int tAction = ev.getAction();
		
		if (tAction == MotionEvent.ACTION_DOWN || tAction == MotionEvent.ACTION_UP || tAction == MotionEvent.ACTION_CANCEL){
			int tCount = this.getChildCount();
			
			if (tCount > 0){
				float tX = ev.getX(), tY = ev.getY();
				View tView = null;
				
				for (int i = tCount - 1; i >= 0; i--){
					if ((tView = this.getChildAt(i)) != null){
						if (tView.getLeft() <= tX && tView.getRight() >= tX && tView.getTop() <= tY && tView.getBottom() >= tY){
							break;
						}else{
							tView = null;
						}
					}
				}
				
				if (tView != null){
					if (tAction == MotionEvent.ACTION_DOWN){
						iHaveTouchedChild = true;
					}else{
						if (iHaveTouchedChild && iDrawAfterTouchChild){
							tView.onTouchEvent(ev);
						}
						
						iHaveTouchedChild = false;
					}
				}
			}
		}
		
		return super.onInterceptTouchEvent(ev);
	}
}
